<?
include_once("curl_lib.php");

extract($_POST);

$portal_id = 1;

if (!isset($only_available)) {
    $only_available = TRUE;
}



if (isset($stock_op)) {
    switch($stock_op) {
    case "info_prod":
        $result = info_prod();
        break;

    case "check_availability":
        $result = check_availability();
        break;

    case "insert_prod":
        $result = insert_prod();
        break;

    case "reserve_prod":
        $result = reserve_prod();
        break;

    case "remove_prod":
        $result = remove_prod();
        break;

    case "search_prod":
        $result = search_prod(1, 1, $terms);
        break;

    default:
        $result =  FALSE;
        break;
    }
}

function info_prod($portal_id = NULL, $product_id = NULL) {
    if ($portal_id == NULL) {
        $portal_id = 1;
        $product_id = $_POST['product_id'];
    }
    $info_prod = "http://rachacuca.org:8000/stock/info/$portal_id/$product_id/";
    $contents = file_get_contents($info_prod);
    $regex_info_prod = "/\{\"price\"\: \"(.*)?\", \"title\"\: \"(.*)?\", \"description\"\: \"(.*)?\", \"weight\"\: \"(.*)?\", \"technical_description\"\: \"(.*)?\"\}/i";
    preg_match($regex_info_prod, $contents, $matches);
    if (!isset($matches[2]) || $matches[2] == '') {
        return FALSE;
    }
    return array(
        'preco' => $matches[1],
        'produto' => $matches[2],
        'descricao' => $matches[3],
        'peso' => $matches[4],
        'detalhes' => $matches[5],
    );
}

function check_availability($portal_id = NULL, $product_id = NULL) {
    if ($portal_id == NULL) {
        $portal_id = 1;
        $product_id = $_POST['product_id'];
    }
    $check_availability = "http://rachacuca.org:8000/stock/availability/$portal_id/$product_id/";
    $contents = file_get_contents($check_availability);
    $regex_check_availability = "/\{\"available\"\: (.*)?, \"reserved\"\: (.*)?\}/i";
    preg_match($regex_check_availability, $contents, $matches);
    if (!isset($matches[1]) || !isset($matches[2])) {
        return FALSE;
    }
    return array(
        'disponivel' => $matches[1],
        'reservado' => $matches[2],
    );
}

function insert_prod($portal_id = NULL, $product_id = NULL, $quant = NULL){
    $insert_prod = "http://rachacuca.org:8000/stock/insert/$portal_id/$product_id/$quant/";
    $contents = http_post_fields($insert_prod, array("portal-id" => $portal_id, "product-id" => $product_id, "quantity" => $quant));
    $regex_insert_prod = "/\{\"return\"\: (.*)?\}/i";
    preg_match($regex_insert_prod, $contents, $matches);
    if ($matches[1] === 'true') {
        $result =  TRUE;
    }
    else {
        $result =  FALSE;
    }
}

function reserve_prod($portal_id = NULL, $product_id = NULL, $quant = NULL){
    $reserve_prod = "http://rachacuca.org:8000/stock/reserve/$portal_id/$product_id/$quant/";
    $contents = http_post_fields($reserve_prod, array("portal-id" => $portal_id, "product-id" => $product_id, "quantity" => $quant));
    $regex_reserve_prod = "/\{\"return\"\: (.*)?\}/i";
    preg_match($regex_reserve_prod, $contents, $matches);
    if ($matches[1] === 'true') {
        $result =  TRUE;
    }
    else {
        $result =  FALSE;
    }
}

function remove_prod($portal_id = NULL, $product_id = NULL, $quant = NULL){
    $remove_prod = "http://rachacuca.org:8000/stock/remove/$portal_id/$product_id/$quant/";
    $contents = http_post_fields($remove_prod, array("portal-id" => $portal_id, "product-id" => $product_id, "quantity" => $quant));
    $regex_remove_prod = "/\{\"return\"\: (.*)?\}/i";
    preg_match($regex_remove_prod, $contents, $matches);
    if ($matches[1] === 'true') {
        $result =  TRUE;
    }
    else {
        $result =  FALSE;
    }
}

function search_prod($portal_id = 1, $only_available = 1, $terms = '') {
    $search_prod = "http://rachacuca.org:8000/stock/search/$portal_id/";
    $request = new HttpRequest($search_prod, HttpRequest::METH_GET);
    $request->addQueryData(array('only-available' => $only_available, 'terms' => $terms));
    try {
        $request->send();
        if ($request->getResponseCode() == 200) {
            $regex_search_prod = "/\{\"result\"\: \[(.*)?\]\}/i";
            preg_match($regex_search_prod, $request->getResponseBody(), $matches);
            if ($matches[1]) {
                $products = explode(',', $matches[1]);
                return $products;
            }
            else {
                return FALSE;
            }
        }
    } catch (HttpException $ex) {
        echo $ex;
    }
}

function reset_prod($portal_id = 1) {
    $reset_prod = "http://rachacuca.org:8000/stock/reset/$portal_id/";
    $contents = http_post_fields($reset_prod, array("portal-id" => $portal_id));
}

function replace_unicode_escape_sequence($match) {
    return mb_convert_encoding(pack('H*', $match[1]), 'UTF-8', 'UCS-2BE');
}
function convert_unicode_string($str) {
    return preg_replace_callback('/\\\\u([0-9a-f]{4})/i', 'replace_unicode_escape_sequence', $str);
}

?>
